setGlobalDuration(-1)
setChannels(1)

#setAudioDevice(inumber=0)
#inputMic(out='mic')
playSound(loop=True, out='mic')

oscReceive(bus=['trig','trig2','trig3','trig4'], address=['/trig','/trig2','/trig3','/trig4'], port=8000)

tab,frac,tab2,frac2,tab3,frac3,tab4,frac4  = recordBuffer(input='mic', 
                                                          trigbus=['trig','trig2','trig3','trig4'],
                                                          bufferlength=1)

example = 1

if example == 1:
    rando(bus=['pos','pos2','pos3','pos4'], mini=0, maxi=[frac,frac2,frac3,frac4])    
    granulator2(table=[tab,tab2,tab3,tab4], overlaps=8, amplitude=.2, pitch=1, 
                pointerpos=1, pointerposVar=['pos','pos2','pos3','pos4'])

if example == 2:
    randomi(bus='pvar', mini=-3, maxi=4)
    granulator(table=[tab,tab2,tab3,tab4], pitch=1, pointerrateVar='pvar')

if example == 3:
    looper(table=[tab,tab2,tab3,tab4], looplength=[frac,frac2,frac3,frac4])

startCsound()

def trig1():
    sendOscTrigger(value=1, address='/trig')

def trig2():
    sendOscTrigger(value=1, address='/trig2')

def trig3():
    sendOscTrigger(value=1, address='/trig3')

def trig4():
    sendOscTrigger(value=1, address='/trig4')

frame = beginGUI(size=(90,190))
id1 = makeButton(frame, label='grain1', pos=(10,10), function=trig1)
id2 = makeButton(frame, label='grain2', pos=(10,50), function=trig2)
id3 = makeButton(frame, label='grain3', pos=(10,90), function=trig3)
id4 = makeButton(frame, label='grain4', pos=(10,130), function=trig4)
endGUI(frame)